﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Aribaccio
{
    /// <summary>
    /// 定义扩展Json序列反序列类
    /// </summary>
    public static class ExtendJsonSerialize
    {
        /// <summary>
        /// 日期格式化
        /// </summary>
        private static IsoDateTimeConverter FormatConvert = new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff" };
     
        /// <summary>
        /// 将实体类序列化为JSON
        /// </summary>
        /// <param name="argObj"></param>
        /// <returns></returns>
        public static string ToJsonString(this object argObj)
        {
            try
            {
                return JsonConvert.SerializeObject(argObj, Formatting.Indented, FormatConvert);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 将实体类序列化为JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argItem"></param>
        /// <returns></returns>
        public static string ToJsonString<T>(this T argItem)
        {
            try
            {
                return JsonConvert.SerializeObject(argItem, Formatting.Indented, FormatConvert);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 反序列化JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argJson"></param>
        /// <returns></returns>
        public static T? ToItem<T>(this string argJson)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(argJson);
            }
            catch (Exception)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 反序列化JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argJson"></param>
        /// <returns></returns>
        public static List<T> ToItems<T>(this string argJson)
        {
            try
            {
                List<T>? RelustData = JsonConvert.DeserializeObject<List<T>>(argJson);
                return RelustData != null ? RelustData : new List<T>();
            }
            catch (Exception)
            {
                return new List<T>();
            }
        }

        /// <summary>
        /// 日期转换为时间戳（时间戳单位秒）
        /// </summary>
        /// <param name="TimeStamp"></param>
        /// <returns></returns>
        public static long ConvertToTimeStamp(DateTime time)
        {
            DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return (long)(time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
        }
        /// <summary>
        /// Json字符串日期类型转换
        /// </summary>
        /// <param name="argJson">Json字符串</param>
        /// <returns>转换后Json字符串</returns>
        public static string ConvertJsonStringToDateTime(string argJson)
        {
            return Regex.Replace(argJson, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
            });
        }
    }
}
